package RegExp;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Created with IntelliJ IDEA.
 * User: Ratuil
 * Date: 18.03.13
 * Time: 0:11
 * To change this template use File | Settings | File Templates.
 */
public class EmailsParser {
    public static void main(String[] args) {
        new EmailsParser().run();
    }
    public void run(){
        Pattern p=null;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter text or nothing :) ");
        String str=null;
        try{
            str=sc.nextLine();
            p=Pattern.compile("(([a-zA-Z][\\w]*)@[\\w[.]]*\\.+([a-z]+))");
        }catch (PatternSyntaxException e){
            System.out.println("Syntax error in pattern");
            System.exit(1);
        }catch (Exception e){
            System.out.println("Something went wrong: ");
            e.printStackTrace();
            System.exit(1);
        }
        if(str.equals("")||str.equals(null)){
            str=txt;
            System.out.println("Using text: "+str);
        }
        Matcher m=p.matcher(str);
        System.out.println(m.groupCount()+" maches found:");
        while(m.find())
            System.out.println(txt.substring(m.start(),m.end()));

    }
    String txt="It is a time of legend.\n" +
            "Mighty heroes battle for the right to rule the galaxy. GlarGar@gmail.com The vast armies of the Emperor of Earth have conquered the galaxy in a Great Crusade – the myriad alien races have been smashed by the Emperor’s elite warriors and wiped from the face of history.\n" +
            "The dawn of a new age of supremacy for humanity beckons.\n" +
            "Gleaming citadels of marble and gold celebrate the many victories Ksel-Naga@yandex.ru of the Emperor. Triumphs are raised on a million worlds to record the epic deeds of his most powerful and deadly warriors.\n" +
            "First and foremost amongst these are the primarchs, superheroic beings who have led the Emperor’s armies of Space Marines in victory after victory. They are unstoppable and magnificent, the pinnacle of the Emperor’s genetic experimentation. The Space Marines are the mightiest human warriors the galaxy has ever known, each capable of besting a hundred normal men or more in combat.\n" +
            "Organised into vast armies of tens of thousands GlarGar5318@mail.ru called Legions, the Space Marines and their primarch leaders conquer the galaxy in the name of the Emperor.\n" +
            "Chief amongst the primarchs is Horus, called the Glorious, the Brightest Star, favourite of the Emperor, and like a son unto him. He is the Warmaster, the commander-in-chief of the Emperor’s military might, subjugator of a thousand worlds and conqueror of the galaxy. He is a warrior without peer, a diplomat supreme.\n" +
            "As the flames of war spread through the Imperium, mankind’s champions will all be put to the ultimate test.";
}
